﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Windows.Forms;
using System.IO;
using fingerWinForm.SVM;
using fingerWinForm.UpdrsTests;
using fingerWinForm.Algorithms.SVM;

namespace fingerWinForm.Excel
{
    public struct PatientDetails
    {
        public string id;
        public string firstName;
        public string lastName;
        public string birthDate;
        public string gender;
        public string phoneNumber;
    }
    public struct objForgenerateRandomVectorFile
    {
        public int index;
        public float[] fingerMaxArray;
        public float[] fingerMinArray;
        public float[] handMaxArray;
        public float[] handMinArray;
    }  
    public class FileMgr
    {
        private Microsoft.Office.Interop.Excel.Application papp = null;

        private Microsoft.Office.Interop.Excel.Application patientGradeSheet = null;
        private Microsoft.Office.Interop.Excel.Workbook patientGradeWorkbookRawData = null;
        private Microsoft.Office.Interop.Excel.Worksheet patientWorksheetRawData = null;

        private Microsoft.Office.Interop.Excel.Workbook workbook = null;
        private Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
        private Microsoft.Office.Interop.Excel.Range range = null;
        private Microsoft.Office.Interop.Excel.Range last = null;

        private string testName = null;

        public delegate void trainFileWasCreated(string path);

        public event trainFileWasCreated eTrainFileWasCreated;
        
        public void ceatePatientsFile()
        {
            try
            {
                //app = new Microsoft.Office.Interop.Excel.Application();
                //app.DefaultSheetDirection = (int)Microsoft.Office.Interop.Excel.Constants.xlLTR;
                //app.Visible = true;
                workbook = papp.Workbooks.Add(1);
                worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1];
                worksheet.DisplayRightToLeft = false;

                range = worksheet.get_Range("A1", "I2000");
                range.ColumnWidth = 10; //set the width of the columns automatically 
                worksheet.Cells[1, 1].Interior.ColorIndex = 36;
                worksheet.Cells[1, 2].Interior.ColorIndex = 36;
                worksheet.Cells[1, 3].Interior.ColorIndex = 36;
                worksheet.Cells[1, 4].Interior.ColorIndex = 36;
                worksheet.Cells[1, 5].Interior.ColorIndex = 36;
                worksheet.Cells[1, 6].Interior.ColorIndex = 36;

                worksheet.Cells[1, 1] = "ID:";
                worksheet.Cells[1, 2] = "First Name:";
                worksheet.Cells[1, 3] = "Last Name:";
                worksheet.Cells[1, 4] = "Age:";
                worksheet.Cells[1, 5] = "Gender:";
                worksheet.Cells[1, 6] = "Phone Number:";
            }
            catch (Exception e)
            {
                Console.Write("Error in excel usage -> ceatePatientsFile");
            }
            workbook.SaveAs(MainWindow.startingPath+"patients\\patients.xlsx", Type.Missing, Type.Missing, // save the excel file
                Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        }
        public void patientsInitialize()
        {
            workbook = papp.Workbooks.Open(MainWindow.startingPath + "patients\\patients.xlsx",
                    Type.Missing, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                    Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1];
        }
        public void addNewPatientDetails(PatientDetails details)
        {
            PatientDetails searchData = findPatientDetails(details.id);
            if (searchData.id != null)
            {
                MessageBox.Show("This Id is already in the records", "My Application",
                MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else //new patient.
            {
                try
                {
 
                    worksheet.DisplayRightToLeft = false;
                }
                catch (Exception e)
                {
                    Console.Write("Error in excel usage -> addNewPatientDetails");
                }
                range = worksheet.get_Range("A1:A2000", Type.Missing);
                range.Columns.AutoFit(); //set the width of the columns automatically 


                last = worksheet.Cells.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
                range = worksheet.get_Range("A1", last);

                int lastUsedRow = last.Row;

                worksheet.Cells.NumberFormat = "@";
                worksheet.Cells[lastUsedRow + 1, 1] = details.id;
                worksheet.Cells[lastUsedRow + 1, 2] = details.firstName;
                worksheet.Cells[lastUsedRow + 1, 3] = details.lastName;
                worksheet.Cells[lastUsedRow + 1, 4] = details.birthDate;
                worksheet.Cells[lastUsedRow + 1, 5] = details.gender;
                worksheet.Cells[lastUsedRow + 1, 6] = details.phoneNumber;

                workbook.Save();

                createResualtsExcelSheet(details);
            }
        }
        public void createResualtsExcelSheet(PatientDetails details)
        {
            Microsoft.Office.Interop.Excel.Application app2 = null;
            Microsoft.Office.Interop.Excel.Workbook workbookRawData = null;
            Microsoft.Office.Interop.Excel.Worksheet worksheetRawData = null;

            //string time = DateTime.Now.ToString("HHmmss", System.Globalization.DateTimeFormatInfo.InvariantInfo);
            string date = DateTime.Now.ToString("ddMMyyyy");

            try
            {
                app2 = new Microsoft.Office.Interop.Excel.Application();
                app2.DefaultSheetDirection = (int)Microsoft.Office.Interop.Excel.Constants.xlCenter;
                //app2.Visible = true;
                workbookRawData = app2.Workbooks.Add(1);
                worksheetRawData = (Microsoft.Office.Interop.Excel.Worksheet)workbookRawData.Sheets[1];
                worksheetRawData.DisplayRightToLeft = false;
            }
            catch (Exception e)
            {
                Console.Write("Error in excel usage when trying to create results excel sheet ");
            }
            finally
            {
            }
            //=================building the excel sheet==============================//
            range = worksheetRawData.get_Range("A1", "N2000");
            range.ColumnWidth = 16; //set the width of the columns automatically 

            for (int i = 1; i < 13; i++)
            {
                worksheetRawData.Cells[1, i].Interior.ColorIndex = 36;
            }
            
            worksheetRawData.Cells[1, 1] = "ID:";
            worksheetRawData.Cells[1, 2] = details.id;
            worksheetRawData.Cells[1, 3] = "First Name:";
            worksheetRawData.Cells[1, 4] = details.firstName;
            worksheetRawData.Cells[1, 5] = "Last Name:";
            worksheetRawData.Cells[1, 6] = details.lastName;
            worksheetRawData.Cells[1, 7] = "Birth Date";
            worksheetRawData.Cells[1, 8] = details.birthDate;
            worksheetRawData.Cells[1, 9] = "Gender:";
            worksheetRawData.Cells[1, 10] = details.gender;
            worksheetRawData.Cells[1, 11] = "Phone number";
            worksheetRawData.Cells.NumberFormat = "@";
            worksheetRawData.Cells[1, 12] = details.phoneNumber;

            worksheetRawData.Cells[3, 1] = "Date:";
            worksheetRawData.Cells[4, 2] = "Grade:";
            worksheetRawData.Cells[4, 1] = "Test name:";

            range = worksheetRawData.get_Range("A1", "A70");
            range.ColumnWidth = 29; //set the width of the columns automatically 

            worksheetRawData.Cells[5, 1] = "1. Mentation";
            worksheetRawData.Cells[6, 1] = "2. Thought Disorder";
            worksheetRawData.Cells[7, 1] = "3. Depression";
            worksheetRawData.Cells[8, 1] = "4. Motivation/Initiative";
            worksheetRawData.Cells[9, 1] = "5. Speech";
            worksheetRawData.Cells[10, 1] = "6. Salivation:";
            worksheetRawData.Cells[11, 1] = "7. Swallowing:";
            worksheetRawData.Cells[12, 1] = "8. Handwriting";
            worksheetRawData.Cells[13, 1] = "9. Cutting food";
            worksheetRawData.Cells[14, 1] = "10. Dressing";
            worksheetRawData.Cells[15, 1] = "11. Hygiene";
            worksheetRawData.Cells[16, 1] = "12. Turning in bed";
            worksheetRawData.Cells[17, 1] = "13. Falling";
            worksheetRawData.Cells[18, 1] = "14. Freezing";
            worksheetRawData.Cells[19, 1] = "15. Walking";
            worksheetRawData.Cells[20, 1] = "16. Tremor";
            worksheetRawData.Cells[21, 1] = "17. Sensory symptoms";
            worksheetRawData.Cells[22, 1] = "18. Speech";
            worksheetRawData.Cells[23, 1] = "19. Facial expression";
            worksheetRawData.Cells[24, 1] = "20. Tremor at rest: face,lips,chin";
            worksheetRawData.Cells[25, 1] = "Hands: right";
            worksheetRawData.Cells[26, 1] = "left";
            worksheetRawData.Cells[27, 1] = "Feet: right";
            worksheetRawData.Cells[28, 1] = "left";
            worksheetRawData.Cells[29, 1] = "21. Action tremor: right";
            worksheetRawData.Cells[30, 1] = "left";
            worksheetRawData.Cells[31, 1] = "22. Rigidity: neck";
            worksheetRawData.Cells[32, 1] = "Upper extremity: right";
            worksheetRawData.Cells[33, 1] = "left";
            worksheetRawData.Cells[34, 1] = "Lower extremity: right";
            worksheetRawData.Cells[35, 1] = "left";
            worksheetRawData.Cells[36, 1] = "23. Finger taps: right";
            worksheetRawData.Cells[37, 1] = "left";
            worksheetRawData.Cells[38, 1] = "24. hand Movements: right";
            worksheetRawData.Cells[39, 1] = "left";
            worksheetRawData.Cells[40, 1] = "25. Hand pronate/supinate: right";
            worksheetRawData.Cells[41, 1] = "25. left";
            worksheetRawData.Cells[42, 1] = "26. Leg agility: right";
            worksheetRawData.Cells[43, 1] = "left";
            worksheetRawData.Cells[44, 1] = "27. Arise from chair";
            worksheetRawData.Cells[45, 1] = "28. Posture";
            worksheetRawData.Cells[46, 1] = "29. Gait";
            worksheetRawData.Cells[47, 1] = "30. Postural stability";
            worksheetRawData.Cells[48, 1] = "31. Body bradykinesia";
            worksheetRawData.Cells[49, 1] = "32. Dyskinesia (duration)";
            worksheetRawData.Cells[50, 1] = "33. Dyskinesia (disability)";
            worksheetRawData.Cells[51, 1] = "34. Dyskinesia (pain)";
            worksheetRawData.Cells[52, 1] = "35. Early morning dystonia";
            worksheetRawData.Cells[53, 1] = "36. 'Offs' (predictable)";
            worksheetRawData.Cells[54, 1] = "37. 'Offs' (unpredictable)";
            worksheetRawData.Cells[55, 1] = "38. 'Offs' (sudden)";
            worksheetRawData.Cells[56, 1] = "39. 'Offs' (duration)";
            worksheetRawData.Cells[57, 1] = "40. Anorexia, nausea, vomiting";
            worksheetRawData.Cells[58, 1] = "41. Sleep disturbance";
            worksheetRawData.Cells[59, 1] = "42. Symptomatic orthostasis";
            worksheetRawData.Cells[60, 1] = "Blood Pressure: seated";
            worksheetRawData.Cells[61, 1] = "supine";
            worksheetRawData.Cells[62, 1] = "standing";
            worksheetRawData.Cells[63, 1] = "Weight";
            worksheetRawData.Cells[64, 1] = "Pulse: seated";
            worksheetRawData.Cells[65, 1] = "Standing";
            worksheetRawData.Cells[66, 1] = "Name of examiner";
            worksheetRawData.Cells[67, 1] = "Hoehn & Yahr Stage";
            worksheetRawData.Cells[68, 1] = "% ADL Score (PD)";
            worksheetRawData.Cells[69, 1] = "% ADL (with dyskinesia)";

            //==================================================================//

            //===============Saving the excel file and closing it===============//
            string dirpath = MainWindow.startingPath + "patients\\" + details.id ;
            this.CreateIfMissing(dirpath);

            workbookRawData.SaveAs(dirpath + "\\" + details.id + ".xlsx", Type.Missing, Type.Missing, // save the excel file
                    Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive,
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            object misValue = System.Reflection.Missing.Value;
            workbookRawData.Close(true, misValue, misValue);
            app2.Application.Quit();
            app2 = null;

        }
        public PatientDetails findPatientDetails(string id)
        {
            PatientDetails details = new PatientDetails();

            range = worksheet.get_Range("A1:A2000", Type.Missing);
            range.Columns.AutoFit(); //set the width of the columns automatically 
            Microsoft.Office.Interop.Excel.Range currentFind = null;

            //find the row of the id entered
            currentFind = this.range.Find(id, Type.Missing,
            Microsoft.Office.Interop.Excel.XlFindLookIn.xlValues, Microsoft.Office.Interop.Excel.XlLookAt.xlWhole,
            Microsoft.Office.Interop.Excel.XlSearchOrder.xlByColumns, Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, false,
            Type.Missing, Type.Missing);

            if (currentFind != null)
            {
                range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[currentFind.Row, 1];
                if(range.Value!=null)
                details.id = range.Value.ToString();

                range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[currentFind.Row, 2];
                if (range.Value != null)
                details.firstName = range.Value.ToString();

                range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[currentFind.Row, 3];
                if (range.Value != null)
                details.lastName = range.Value.ToString();

                range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[currentFind.Row, 4];
                if (range.Value != null)
                details.birthDate = range.Value.ToString();

                range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[currentFind.Row, 5];
                if (range.Value != null)
                details.gender = range.Value.ToString();

                range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[currentFind.Row, 6];
                if (range.Value != null)
                details.phoneNumber = range.Value.ToString();

                //workbook.Close();
                //app.Quit();
                //app = null;
            }
            return details;
        }
        public void updateGradeForTest(string grade, string filePath,int numberOftest) //file path without the .*
        {
            string[] stringforcutting = filePath.Split('\\');
            string id = stringforcutting[5];

            Microsoft.Office.Interop.Excel.Range range = null;
            string tempstring =null;
            //update grade in test excel file
            //try
            //{
            //    app2 = new Microsoft.Office.Interop.Excel.Application();
            //    app2.DefaultSheetDirection = (int)Microsoft.Office.Interop.Excel.Constants.xlLTR;
            //    //app2.Visible = true;
            //    app2.DisplayAlerts = false;
            //    workbookRawData = app2.Workbooks.Open(filePath + ".xlsx",
            //        Type.Missing, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            //        Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            //    worksheetRawData = (Microsoft.Office.Interop.Excel.Worksheet)workbookRawData.Sheets[1];
            //    worksheetRawData.DisplayRightToLeft = false;

            //    worksheetRawData = (Microsoft.Office.Interop.Excel.Worksheet)workbookRawData.Sheets[1];
            //    worksheetRawData.DisplayRightToLeft = false;
            //}
            //catch (Exception e)
            //{
            //    Console.Write("Error in updating grade in of test in excel file");
            //}
            
            //worksheetRawData.Cells[3, 9] = grade;
            //workbookRawData.SaveAs(filePath + ".xlsx", Type.Missing, Type.Missing, // save the excel file
            //        Type.Missing, false, Type.Missing, XlSaveAsAccessMode.xlExclusive,
            //       XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            //object misValue = System.Reflection.Missing.Value;
            //workbookRawData.Close(true, misValue, misValue);
            //app2.Application.Quit();
            //app2 = null;

            //update grade in text file
            //string currentContent = String.Empty;
            //if (File.Exists(filePath + ".text"))
            //{
            //    currentContent = File.ReadAllText(filePath+".text");
            //}
            //File.WriteAllText(filePath+".text", grade + currentContent);

            //update grade in the patient grade sheet
            filePath = null;
            filePath = @"C:\parkinsonTool\patients\" + id + "\\"+ id +".xlsx";
            try
            {
                if (patientGradeSheet == null)
                {
                    patientGradeSheet = new Microsoft.Office.Interop.Excel.Application();
                    patientGradeSheet.DefaultSheetDirection = (int)Microsoft.Office.Interop.Excel.Constants.xlLTR;
                    


                    patientGradeSheet.DisplayAlerts = false;
                    patientGradeWorkbookRawData = patientGradeSheet.Workbooks.Open(filePath,
                        Type.Missing, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                        Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

                    patientWorksheetRawData = (Microsoft.Office.Interop.Excel.Worksheet)patientGradeWorkbookRawData.Sheets[1];
                    patientWorksheetRawData.DisplayRightToLeft = false;
                    
                }              
                    //patientGradeSheet.ActiveWindow.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMinimized;
                    patientGradeSheet.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMinimized;
                    patientGradeSheet.Visible = true;
  
                
            }
            catch (Exception e)
            {
                Console.Write("Error in updating grade in of test in excel file");
            }

            int index = 2;
            range = patientWorksheetRawData.UsedRange;
            
            int rowCount = range.Rows.Count;
            string date = Convert.ToString(DateTime.Now.ToString("ddMMyyyy"));
            tempstring = Convert.ToString(range.Value2[3, index]);
            while (tempstring != null && tempstring != date)
            {
                index++;
                tempstring = Convert.ToString(range.Value2[3, index]);
            }
            patientWorksheetRawData.Cells.NumberFormat = "@";
            patientWorksheetRawData.Cells[3, index] = date;

            if (Convert.ToString(range.Value2[(numberOftest + 13), index]) == null)
            {
                patientWorksheetRawData.Cells[(numberOftest + 13), index] = Convert.ToInt32(grade);
            }
            else
            {
                patientWorksheetRawData.Cells[(numberOftest + 14), index] = Convert.ToInt32(grade);
            }

            patientWorksheetRawData.SaveAs(@"C:\parkinsonTool\patients\" + id + "\\" + id + ".xlsx",
                            Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            

        }
        public FileMgr()
        {
            papp = new Microsoft.Office.Interop.Excel.Application();
            papp.DefaultSheetDirection = (int)Microsoft.Office.Interop.Excel.Constants.xlLTR;
        }
        public void closeFileMgr()
        {
            if (workbook != null && worksheet != null)
            {
                //workbook.Save(); //Tal Waserman this line caused a bug 
                worksheet = null;
                workbook = null;
            }
            if(papp != null)
            {
                papp.Quit();
                papp = null;
            }

        }
        private bool checkedTheValueIsNotNull(string testName,TrainingModetestGrades trainingGrades)
        {
            switch (testName)
            {
                case "fingerTaps":
                    if (trainingGrades.LfingerTaps == null)
                        return false;
                    if (trainingGrades.RfingerTaps == null)
                        return false;
                break;

                case "handMovments":
                    if (trainingGrades.LhandMovments == null)
                        return false;
                    if (trainingGrades.RhandMovments == null)
                        return false;
                break;
            }
            return true;
        }
        private void createPatientDir(string id)
        {
            if (!System.IO.Directory.Exists(MainWindow.startingPath + "rawData\\" + id))
            {
                System.IO.Directory.CreateDirectory(MainWindow.startingPath + "rawData\\testing\\" + id);
                System.IO.Directory.CreateDirectory(MainWindow.startingPath + "rawData\\testing\\" + id + "\\finger Taps");
                System.IO.Directory.CreateDirectory(MainWindow.startingPath + "rawData\\testing\\" + id + "\\hand Movements");
            }

        }
        static public void loadDataFromText(ref objectForRead DataFromExcel)
        {
            //StreamReader  tw = new StreamReader(DataFromExcel.path);  //open the text file
            CenterOfHandStruct tempCenterStruct = new CenterOfHandStruct();
            FingerTipsStruct tempFingerTipStruct = new FingerTipsStruct();
            //String temp;

            string[] lines = System.IO.File.ReadAllLines(DataFromExcel.path);
            int lineCount = lines.Count();
            string[] values;
            int flag1 = 0;

            values = lines[0].Split(' ');
            if (values.Count() == 1 && values[0]!="")
            {
                DataFromExcel.grade = Convert.ToDouble(values[0]);
                for (int i = 1; i < lineCount; i++)
                {
                    values = lines[i].Split(' ');
                    if (values[0] != "center:" && values[0] != "FingerTips:" && flag1 == 0)
                    {
                        tempCenterStruct.X = Convert.ToInt32(values[0]);
                        tempCenterStruct.Y = Convert.ToInt32(values[1]);
                        tempCenterStruct.timeStemp = Convert.ToDouble(values[2]);
                        DataFromExcel.centerOfHandListForSaving.Add(tempCenterStruct);
                    }

                    if (values[0] != "center:" && values[0] != "FingerTips:" && flag1 == 1)
                    {
                        tempFingerTipStruct.X = Convert.ToInt32(values[0]);
                        tempFingerTipStruct.Y = Convert.ToInt32(values[1]);
                        tempFingerTipStruct.timeStemp = Convert.ToDouble(values[2]);
                        DataFromExcel.fingerTipsListForSaving.Add(tempFingerTipStruct);
                    }

                    if (values[0] == "FingerTips:")
                    {
                        flag1 = 1;
                    }

                }
            }
            else 
            {
                for (int i = 1; i < lineCount; i++)
                {
                        values = lines[i].Split(' ');
                        if (values[0] != "center:" && values[0] != "FingerTips:" && flag1 == 0)
                        {
                            tempCenterStruct.X = Convert.ToInt32(values[0]);
                            tempCenterStruct.Y = Convert.ToInt32(values[1]);
                            tempCenterStruct.timeStemp = Convert.ToDouble(values[2]);
                            DataFromExcel.centerOfHandListForSaving.Add(tempCenterStruct);
                        }

                        if (values[0] != "center:" && values[0] != "FingerTips:" && flag1 == 1)
                        {
                            tempFingerTipStruct.X = Convert.ToInt32(values[0]);
                            tempFingerTipStruct.Y = Convert.ToInt32(values[1]);
                            tempFingerTipStruct.timeStemp = Convert.ToDouble(values[2]);
                            DataFromExcel.fingerTipsListForSaving.Add(tempFingerTipStruct);
                        }

                        if (values[0] == "FingerTips:")
                        {
                            flag1 = 1;
                        }
                    

                }
            }
            

            
            
        }
        public void loadDataFromExcel(ref objectForRead DataFromExcel)
        {
            Microsoft.Office.Interop.Excel.Application app = null;
            Microsoft.Office.Interop.Excel.Workbook workbookRawData = null;
            Microsoft.Office.Interop.Excel.Worksheet worksheetRawData = null;
            Microsoft.Office.Interop.Excel.Range range = null;

            CenterOfHandStruct tempCenterStruct = new CenterOfHandStruct();
            FingerTipsStruct tempFingerTipStruct = new FingerTipsStruct();

            
            //open the excel file
            try
            {
                app = new Microsoft.Office.Interop.Excel.Application();
                app.DefaultSheetDirection = (int)Microsoft.Office.Interop.Excel.Constants.xlLTR;
                //app2.Visible = true;
                workbookRawData = app.Workbooks.Open(DataFromExcel.path,
                    Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                    Type.Missing, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

                worksheetRawData = (Microsoft.Office.Interop.Excel.Worksheet)workbookRawData.Sheets[1];
                worksheetRawData.DisplayRightToLeft = false;
            }
            catch (Exception e)
            {
                Console.Write("Error in loadDataFromExcel");
            }

            int index = 3;
            range = worksheetRawData.UsedRange;

            int rowCount = range.Rows.Count;

            while (range.Value2[index, 2] != null)
            {
                //double temp1 = range.Value2[index, 2];

                tempCenterStruct.X = Convert.ToInt32(range.Value2[index, 2]);
                tempCenterStruct.Y = Convert.ToInt32(range.Value2[index, 3]);
                tempCenterStruct.timeStemp = Convert.ToDouble(range.Value2[index, 4]);
                DataFromExcel.centerOfHandListForSaving.Add(tempCenterStruct);
                index++;
                if (index > rowCount)
                    break;
            }

            index = 3;

            while (range.Value2[index, 6] != null)
            {
                tempFingerTipStruct.X = Convert.ToInt32(range.Value2[index, 6]);
                tempFingerTipStruct.Y = Convert.ToInt32(range.Value2[index, 7]);
                tempFingerTipStruct.timeStemp = Convert.ToDouble(range.Value2[index, 8]);
                DataFromExcel.fingerTipsListForSaving.Add(tempFingerTipStruct);
                index++;
                if (index > rowCount)
                    break;
            }
            DataFromExcel.grade = Convert.ToDouble(range.Value2[3, 9]); //find the index in next run...

            workbookRawData.Close();
            app.Application.Quit();
            app = null;

        }
        public void computTrainData(objectForRead DataFromExcel)
        {
            Microsoft.Office.Interop.Excel.Application app = null;
            Microsoft.Office.Interop.Excel.Workbook workbookRawData = null;
            Microsoft.Office.Interop.Excel.Worksheet worksheetRawData = null;
            Microsoft.Office.Interop.Excel.Range range = null;
            FingerTipsStruct temp1;
            FingerTipsStruct temp2;
            FingerTipsStruct temp3;

            //open the excel file
            try
            {
                app = new Microsoft.Office.Interop.Excel.Application();
                app.DefaultSheetDirection = (int)Microsoft.Office.Interop.Excel.Constants.xlLTR;
                //app.Visible = true;
                workbookRawData = app.Workbooks.Open(DataFromExcel.path,
                    Type.Missing, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                    Type.Missing, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

                workbookRawData.Worksheets.Add();
                worksheetRawData = (Microsoft.Office.Interop.Excel.Worksheet)workbookRawData.Sheets[1];
                worksheetRawData.DisplayRightToLeft = false;
            }
            catch (Exception e)
            {
                Console.Write("Error in computTrainData");
            }

            //set the hadline of the sorksheet
            range = worksheetRawData.get_Range("A1", "I2000");
            range.ColumnWidth = 14; //set the width of the columns automatically 

            worksheetRawData.Cells[2, 1].Interior.ColorIndex = 36;
            worksheetRawData.Cells[2, 2].Interior.ColorIndex = 36;


            worksheetRawData.Cells[1, 1] = "test resualts:";
            worksheetRawData.Cells[2, 1] = "Destance:";
            worksheetRawData.Cells[2, 2] = "T:";
                    
            int index = 0;
            int row = 3;
            double check = 0;
            for (int i = 0; (i < DataFromExcel.fingerTipsListForSaving.Count - 3); i++)
            {
                temp1 = DataFromExcel.fingerTipsListForSaving[index];
                temp2 = DataFromExcel.fingerTipsListForSaving[index+1];
                temp3 = DataFromExcel.fingerTipsListForSaving[index+2];

                if (temp1.Y == -1)
                {
                    worksheetRawData.Cells[row, 1] = 0;
                    worksheetRawData.Cells[row, 2] = temp1.timeStemp;
                    row++;
                    index++;
                }
                else if ((temp1.timeStemp == temp2.timeStemp) && (temp1.timeStemp != temp3.timeStemp))
                {
                    check = Math.Sqrt(Math.Pow((temp1.Y - temp2.Y),2)+Math.Pow((temp1.X - temp2.X),2));
                    //check = Math.Abs(temp1.Y - temp2.Y);
                    if (check <= 80)
                    {
                        worksheetRawData.Cells[row, 1] = check;
                    }
                                
                    worksheetRawData.Cells[row, 2] = temp1.timeStemp;

                    index += 2;
                    row++;       
                            
                }
                else
                {
                    index++;
                }
                if ((index + 2) >= DataFromExcel.fingerTipsListForSaving.Count)
                break;
            }

            //build graphs
            Microsoft.Office.Interop.Excel.ChartObjects xlCharts = (Microsoft.Office.Interop.Excel.ChartObjects)worksheetRawData.ChartObjects(Type.Missing);
            Microsoft.Office.Interop.Excel.ChartObject myChart = (Microsoft.Office.Interop.Excel.ChartObject)xlCharts.Add(150, 20, 1000, 250);
            Microsoft.Office.Interop.Excel.Chart chartPage = myChart.Chart;

            Microsoft.Office.Interop.Excel.Range TempRange = worksheetRawData.UsedRange;
            string finalCell = "A" + TempRange.Rows.Count.ToString();
            Microsoft.Office.Interop.Excel.Range chartRange;
            chartRange = worksheetRawData.get_Range("A2", finalCell);
            chartPage.SetSourceData(chartRange, Microsoft.Office.Interop.Excel.XlRowCol.xlColumns);
            chartPage.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlXYScatter;
            //Add treadline
            //Microsoft.Office.Interop.Excel.Series series = (Microsoft.Office.Interop.Excel.Series)chartPage.SeriesCollection(1);
            //Microsoft.Office.Interop.Excel.Trendlines trendlines = (Microsoft.Office.Interop.Excel.Trendlines)series.Trendlines(System.Type.Missing);
            //Microsoft.Office.Interop.Excel.Trendline newTrendline = trendlines.Add(
            //Microsoft.Office.Interop.Excel.XlTrendlineType.xlMovingAvg, 1,
            //System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing,
            //true, true, System.Type.Missing);
            //newTrendline.Select();
            //-------------------------------//
                    
            workbookRawData.Save();
            workbookRawData.Close();
            app.Application.Quit();
            app = null;
        }
        public void putDataToText(object objTransfer,string testName)
        {
            objectForRead objTransferTemp = (objectForRead)objTransfer;
            TextWriter tw;
            string temp = objTransferTemp.path;
            string[] values = temp.Split('.');
            string newPath = values[0] + ".text";

            tw = new StreamWriter(newPath);

            tw.WriteLine(objTransferTemp.grade);
                tw.WriteLine("center: X: Y: TimeStamp:");
                foreach (CenterOfHandStruct iterator in objTransferTemp.centerOfHandListForSaving)
                {
                    tw.WriteLine(iterator.X + " " + iterator.Y + " " + iterator.timeStemp);
                }

                tw.WriteLine("FingerTips: X: Y: TimeStamp:");
                foreach (FingerTipsStruct iterator in objTransferTemp.fingerTipsListForSaving)
                {
                    tw.WriteLine(iterator.X + " " + iterator.Y + " " + iterator.timeStemp);
                }


                tw.Close();
                
            
        }
        public void putDataToFiles(object objTransfer)
        {
            Microsoft.Office.Interop.Excel.Application app2 = null;   
            Microsoft.Office.Interop.Excel.Workbook workbookRawData = null;
            Microsoft.Office.Interop.Excel.Worksheet worksheetRawData = null;
            objectForTransfer objTransferTemp = (objectForTransfer)objTransfer;
            TextWriter tw;

            if (objTransferTemp.currentMode == Mode.TRAINING_MODE) //Trainning mode
            {
                string dirpath = MainWindow.startingPath + "rawData\\training\\" + objTransferTemp.testName + "\\" + objTransferTemp.id;
                this.CreateIfMissing(dirpath);
                tw = new StreamWriter(dirpath + "\\" + objTransferTemp.fileName + ".text");

                if (checkedTheValueIsNotNull(objTransferTemp.testName, MainWindow.trainingGrades) == true)
                {
                    try
                    {
                        app2 = new Microsoft.Office.Interop.Excel.Application();
                        app2.DefaultSheetDirection = (int)Microsoft.Office.Interop.Excel.Constants.xlLTR;
                        //app2.Visible = true;
                        workbookRawData = app2.Workbooks.Add(1);
                        worksheetRawData = (Microsoft.Office.Interop.Excel.Worksheet)workbookRawData.Sheets[1];
                        worksheetRawData.DisplayRightToLeft = false;
                    }
                    catch (Exception e)
                    {
                        Console.Write("Error in excel usage");
                    }
                    finally
                    {
                    }
                    range = worksheetRawData.get_Range("A1", "I2000");
                    range.ColumnWidth = 10; //set the width of the columns automatically 

                    worksheetRawData.Cells[2, 1].Interior.ColorIndex = 36;
                    worksheetRawData.Cells[2, 2].Interior.ColorIndex = 36;
                    worksheetRawData.Cells[2, 3].Interior.ColorIndex = 36;
                    worksheetRawData.Cells[2, 4].Interior.ColorIndex = 36;
                    worksheetRawData.Cells[2, 5].Interior.ColorIndex = 36;
                    worksheetRawData.Cells[2, 6].Interior.ColorIndex = 36;
                    worksheetRawData.Cells[2, 7].Interior.ColorIndex = 36;
                    worksheetRawData.Cells[2, 8].Interior.ColorIndex = 36;
                    worksheetRawData.Cells[2, 9].Interior.ColorIndex = 36;

                    worksheetRawData.Cells[1, 1] = objTransferTemp.testName;
                    worksheetRawData.Cells[2, 1] = "center:";
                    worksheetRawData.Cells[2, 2] = "X:";
                    worksheetRawData.Cells[2, 3] = "Y:";
                    worksheetRawData.Cells[2, 4] = "TimeStamp:";
                    worksheetRawData.Cells[2, 5] = "FingerTips:";
                    worksheetRawData.Cells[2, 6] = "X:";
                    worksheetRawData.Cells[2, 7] = "Y:";
                    worksheetRawData.Cells[2, 8] = "TimeStamp:";
                    worksheetRawData.Cells[2, 9] = "Grade:";

                    switch (objTransferTemp.testName)
                    {
                        case "Finger Taps":
                            if (MainWindow.trainingGrades.LfingerTaps != null)
                            {
                                worksheetRawData.Cells[3, 9] = MainWindow.trainingGrades.LfingerTaps;
                                tw.WriteLine(MainWindow.trainingGrades.LfingerTaps);
                                MainWindow.trainingGrades.LfingerTaps = null;

                            }
                            else if (MainWindow.trainingGrades.RfingerTaps != null)
                            {
                                worksheetRawData.Cells[3, 9] = MainWindow.trainingGrades.RfingerTaps;
                                tw.WriteLine(MainWindow.trainingGrades.RfingerTaps);
                                MainWindow.trainingGrades.RfingerTaps = null;

                            }
                            break;
                        case "Hand Movements":
                            if (MainWindow.trainingGrades.LhandMovments != null)
                            {
                                worksheetRawData.Cells[3, 9] = MainWindow.trainingGrades.LhandMovments;
                                tw.WriteLine(MainWindow.trainingGrades.LhandMovments);
                                MainWindow.trainingGrades.LhandMovments = null;

                            }

                            else if (MainWindow.trainingGrades.RhandMovments != null)
                            {
                                worksheetRawData.Cells[3, 9] = MainWindow.trainingGrades.RhandMovments;
                                tw.WriteLine(MainWindow.trainingGrades.RhandMovments);
                                MainWindow.trainingGrades.RhandMovments = null;

                            }

                            break;

                    }//end of switch



                    int indexRow = 3;
                    int indexColl = 2;
                    tw.WriteLine("center: X: Y: TimeStamp:");
                    foreach (CenterOfHandStruct iterator in objTransferTemp.centerOfHandListForSaving)
                    {
                        tw.WriteLine(iterator.X + " " + iterator.Y + " " + iterator.timeStemp);
                        worksheetRawData.Cells[indexRow, indexColl] = iterator.X;
                        worksheetRawData.Cells[indexRow, indexColl + 1] = iterator.Y;
                        worksheetRawData.Cells[indexRow, indexColl + 2] = iterator.timeStemp;
                        indexRow++;
                    }

                    indexRow = 3;
                    indexColl = 6;
                    tw.WriteLine("FingerTips: X: Y: TimeStamp:");
                    foreach (FingerTipsStruct iterator in objTransferTemp.fingerTipsListForSaving)
                    {
                        tw.WriteLine(iterator.X + " " + iterator.Y + " " + iterator.timeStemp);
                        worksheetRawData.Cells[indexRow, indexColl] = iterator.X;
                        worksheetRawData.Cells[indexRow, indexColl + 1] = iterator.Y;
                        worksheetRawData.Cells[indexRow, indexColl + 2] = iterator.timeStemp;
                        indexRow++;

                    }


                    tw.Close();
                    workbookRawData.SaveAs(dirpath + "\\" + objTransferTemp.fileName + ".xlsx", Type.Missing, Type.Missing, // save the excel file
                    Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive,
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

                    object misValue = System.Reflection.Missing.Value;
                    workbookRawData.Close(true, misValue, misValue);
                    app2.Application.Quit();
                    app2 = null;
                }
            }
            else //Testing mode
            {
                string dirpath = MainWindow.startingPath + "rawData\\testing\\" + objTransferTemp.testName + "\\" + objTransferTemp.id;
                this.CreateIfMissing(dirpath);
                tw = new StreamWriter(dirpath + "\\" + objTransferTemp.fileName + ".text");

                if (checkedTheValueIsNotNull(objTransferTemp.testName, MainWindow.trainingGrades) == true)
                {
                    try
                    {
                        app2 = new Microsoft.Office.Interop.Excel.Application();
                        app2.DefaultSheetDirection = (int)Microsoft.Office.Interop.Excel.Constants.xlLTR;
                        //app2.Visible = true;
                        workbookRawData = app2.Workbooks.Add(1);
                        worksheetRawData = (Microsoft.Office.Interop.Excel.Worksheet)workbookRawData.Sheets[1];
                        worksheetRawData.DisplayRightToLeft = false;
                    }
                    catch (Exception e)
                    {
                        Console.Write("Error in excel usage");
                    }
                    finally
                    {
                    }
                    range = worksheetRawData.get_Range("A1", "I2000");
                    range.ColumnWidth = 10; //set the width of the columns automatically 

                    worksheetRawData.Cells[2, 1].Interior.ColorIndex = 36;
                    worksheetRawData.Cells[2, 2].Interior.ColorIndex = 36;
                    worksheetRawData.Cells[2, 3].Interior.ColorIndex = 36;
                    worksheetRawData.Cells[2, 4].Interior.ColorIndex = 36;
                    worksheetRawData.Cells[2, 5].Interior.ColorIndex = 36;
                    worksheetRawData.Cells[2, 6].Interior.ColorIndex = 36;
                    worksheetRawData.Cells[2, 7].Interior.ColorIndex = 36;
                    worksheetRawData.Cells[2, 8].Interior.ColorIndex = 36;
                    worksheetRawData.Cells[2, 9].Interior.ColorIndex = 36;

                    worksheetRawData.Cells[1, 1] = objTransferTemp.testName;
                    worksheetRawData.Cells[2, 1] = "center:";
                    worksheetRawData.Cells[2, 2] = "X:";
                    worksheetRawData.Cells[2, 3] = "Y:";
                    worksheetRawData.Cells[2, 4] = "TimeStamp:";
                    worksheetRawData.Cells[2, 5] = "FingerTips:";
                    worksheetRawData.Cells[2, 6] = "X:";
                    worksheetRawData.Cells[2, 7] = "Y:";
                    worksheetRawData.Cells[2, 8] = "TimeStamp:";
                    worksheetRawData.Cells[2, 9] = "Grade:";

                    switch (objTransferTemp.testName)
                    {
                        case "Finger Taps":
                            if (MainWindow.trainingGrades.LfingerTaps != null)
                            {
                                worksheetRawData.Cells[3, 9] = MainWindow.trainingGrades.LfingerTaps;
                                tw.WriteLine(MainWindow.trainingGrades.LfingerTaps);
                                MainWindow.trainingGrades.LfingerTaps = null;

                            }
                            else if (MainWindow.trainingGrades.RfingerTaps != null)
                            {
                                worksheetRawData.Cells[3, 9] = MainWindow.trainingGrades.RfingerTaps;
                                tw.WriteLine(MainWindow.trainingGrades.RfingerTaps);
                                MainWindow.trainingGrades.RfingerTaps = null;

                            }
                            break;
                        case "Hand Movements":
                            if (MainWindow.trainingGrades.LhandMovments != null)
                            {
                                worksheetRawData.Cells[3, 9] = MainWindow.trainingGrades.LhandMovments;
                                tw.WriteLine(MainWindow.trainingGrades.LhandMovments);
                                MainWindow.trainingGrades.LhandMovments = null;

                            }

                            else if (MainWindow.trainingGrades.RhandMovments != null)
                            {
                                worksheetRawData.Cells[3, 9] = MainWindow.trainingGrades.RhandMovments;
                                tw.WriteLine(MainWindow.trainingGrades.RhandMovments);
                                MainWindow.trainingGrades.RhandMovments = null;

                            }

                            break;

                    }//end of switch



                    int indexRow = 3;
                    int indexColl = 2;
                    tw.WriteLine("center: X: Y: TimeStamp:");
                    foreach (CenterOfHandStruct iterator in objTransferTemp.centerOfHandListForSaving)
                    {
                        tw.WriteLine(iterator.X + " " + iterator.Y + " " + iterator.timeStemp);
                        worksheetRawData.Cells[indexRow, indexColl] = iterator.X;
                        worksheetRawData.Cells[indexRow, indexColl + 1] = iterator.Y;
                        worksheetRawData.Cells[indexRow, indexColl + 2] = iterator.timeStemp;
                        indexRow++;
                    }

                    indexRow = 3;
                    indexColl = 6;
                    tw.WriteLine("FingerTips: X: Y: TimeStamp:");
                    foreach (FingerTipsStruct iterator in objTransferTemp.fingerTipsListForSaving)
                    {
                        tw.WriteLine(iterator.X + " " + iterator.Y + " " + iterator.timeStemp);
                        worksheetRawData.Cells[indexRow, indexColl] = iterator.X;
                        worksheetRawData.Cells[indexRow, indexColl + 1] = iterator.Y;
                        worksheetRawData.Cells[indexRow, indexColl + 2] = iterator.timeStemp;
                        indexRow++;

                    }


                    tw.Close();
                    workbookRawData.SaveAs(dirpath + "\\"  + objTransferTemp.fileName + ".xlsx", Type.Missing, Type.Missing, // save the excel file
                    Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive,
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

                    object misValue = System.Reflection.Missing.Value;
                    workbookRawData.Close(true, misValue, misValue);
                    app2.Application.Quit();
                    app2 = null;

                    //create vector file for this test//
                    List<float> vector1 = new List<float>();
                    objForgenerateRandomVectorFile obj2 = new objForgenerateRandomVectorFile();
                    VectorOperations.scaleAraysInit(ref obj2);

                    UTF8Encoding utf8 = new UTF8Encoding();
                    string fileOutPut;

                    tw = new StreamWriter(dirpath + "\\" + objTransferTemp.fileName + ".train");
                    getscalingparamsfromfile(ref obj2, objTransferTemp.testName);
                    VectorOperations.createParamaterVector(objTransferTemp.testName, dirpath + "\\" + objTransferTemp.fileName + ".text",ref vector1,ref obj2);
                    int index = 0;
                    foreach (float it in vector1)
                    {
                        if (index == 0) //for writing the class 
                        {
                            fileOutPut = it + " ";
                            byte[] encodedBytes = utf8.GetBytes(fileOutPut);
                            tw.Write(utf8.GetString(encodedBytes));
                            index++;
                        }
                        else
                        {
                            fileOutPut = index + ":" + it + " ";
                            byte[] encodedBytes = utf8.GetBytes(fileOutPut);
                            tw.Write(utf8.GetString(encodedBytes));
                            index++;
                        }

                    }
                    vector1.Clear();
                    tw.WriteLine(" ");
                    tw.Close();
                    eTrainFileWasCreated(dirpath + "\\" + objTransferTemp.fileName + ".train");
                    //SVMManager.runSVMPredict(dirpath + "\\" + objTransferTemp.fileName + ".train", @"c:\finger Taps_trainfile.train.model",@"c:\aaa.out");
                    //======================================================//
                }
            }

        }
        private void getscalingparamsfromfile(ref objForgenerateRandomVectorFile obj2, string testName)
        {
            bool check = System.IO.File.Exists(@"C:\parkinsonTool\svm" + "\\" + testName + "\\scaleParams.csv");
            if (check)
            {
                string[] lines = System.IO.File.ReadAllLines(@"C:\parkinsonTool\svm"+"\\"+testName+"\\scaleParams.csv");
                string[] line;
                int loopCounter=0;
                int j = 0;

                if (testName == "Finger Taps")
                {
                    loopCounter = VectorOperations.fingerTaps_featureNumber;
                }
                else if (testName == "Hand Movements")
                {
                    loopCounter = VectorOperations.HandMovements_featureNumber;
                }
                else
                {
                    loopCounter = VectorOperations.featureNumber;
                }


                for (int i = 1; i < loopCounter; i++)
                {
                    line = lines[i].Split(',');
                    if (testName == "Finger Taps")
                    {
                        obj2.fingerMaxArray[j] = (float)Convert.ToDouble(line[1]);
                        j++;
                    }
                    else if (testName == "Hand Movements")
                    {
                        obj2.handMaxArray[j] = (float)Convert.ToDouble(line[1]);
                        j++;
                    }
                }

                j = 0;
                for (int i = loopCounter + 2; i < lines.Count(); i++)
                {
                    line = lines[i].Split(',');
                    if (testName == "Finger Taps")
                    {
                        obj2.fingerMinArray[j] = (float)Convert.ToDouble(line[1]);
                        j++;
                    }
                    else if (testName == "Hand Movements")
                    {
                        obj2.handMinArray[j] = (float)Convert.ToDouble(line[1]);
                        j++;
                    }
                }
            }
            

        }        
        public void createTrainfile(string testName , objForgenerateRandomVectorFile obj1)
        {
            string FilePath = @"C:\parkinsonTool\rawData\training\" + testName;
            string[] dirNamesArry = System.IO.Directory.GetDirectories(FilePath); //get all the ID Directories

            string path = @"C:\parkinsonTool\svm\" + testName + "\\";
            string trainFileName = testName+ "_trainfile.train";

            TextWriter tw = new StreamWriter(path + trainFileName);
            objForgenerateRandomVectorFile obj2 = (objForgenerateRandomVectorFile)obj1;

            UTF8Encoding utf8 = new UTF8Encoding();
            string fileOutPut;
            string[] subfilesNamesArry;
            List<float> tempVector = new List<float>();

            foreach (string iterator in dirNamesArry) //train text file
            {
                
                    subfilesNamesArry = System.IO.Directory.GetFiles(iterator, "*.text"); //get all the names of the text files
                    foreach (string tempfileName in subfilesNamesArry) //for each text file do...
                    {
                        VectorOperations.createParamaterVector(testName, tempfileName,ref tempVector,ref obj2);
                        int index = 0;
                        foreach (float it in tempVector)
                        {
                            if (index == 0) //for writing the class 
                            {
                                fileOutPut = it + " ";
                                byte[] encodedBytes = utf8.GetBytes(fileOutPut);
                                tw.Write(utf8.GetString(encodedBytes));
                                index++;
                            }
                            else
                            {
                                fileOutPut = index + ":" + it + " ";
                                byte[] encodedBytes = utf8.GetBytes(fileOutPut);
                                tw.Write(utf8.GetString(encodedBytes));
                                index++;
                            }
                        }
                        tempVector.Clear();
                        tw.WriteLine(" ");

                    } //end of loop

            } //end of loop

            // close the stream
            tw.Close();
        }
        

        //clean a folder from all content
        public static void Empty(System.IO.DirectoryInfo directory)
        {
            foreach (System.IO.FileInfo file in directory.GetFiles()) 
                file.Delete();
            foreach (System.IO.DirectoryInfo subDirectory in directory.GetDirectories()) 
                subDirectory.Delete(true);
        }
        public void CreateIfMissing(string path)
        {
            bool folderExists = System.IO.Directory.Exists(path);
            if (!folderExists)
                System.IO.Directory.CreateDirectory(path);
        }
        public void createDirectoryTree(FileMgr excelManager1)
        {
            if (!System.IO.Directory.Exists(@"c:\parkinsonTool\"))
            {
                //string temp = MainWindow.startingPath;
                System.IO.Directory.CreateDirectory(MainWindow.startingPath); //create the directories
                System.IO.Directory.CreateDirectory(MainWindow.startingPath + "rawData");
                System.IO.Directory.CreateDirectory(MainWindow.startingPath + "rawData\\testing");
                System.IO.Directory.CreateDirectory(MainWindow.startingPath + "rawData\\training");
                System.IO.Directory.CreateDirectory(MainWindow.startingPath + "rawData\\training\\finger Taps");
                System.IO.Directory.CreateDirectory(MainWindow.startingPath + "rawData\\training\\hand Movements");
                System.IO.Directory.CreateDirectory(MainWindow.startingPath + "patients");
                System.IO.Directory.CreateDirectory(MainWindow.startingPath + "svm");
                System.IO.Directory.CreateDirectory(MainWindow.startingPath + "svm\\finger Taps");
                System.IO.Directory.CreateDirectory(MainWindow.startingPath + "svm\\hand Movements");
                excelManager1.ceatePatientsFile();

            }
            else
            {
                excelManager1.patientsInitialize();
            }
        }

    }
}

    

